package com.bandit.controller;

import com.bandit.pojo.Blog;
import com.bandit.pojo.Comment;
import com.bandit.service.impl.BlogServiceImpl;
import com.bandit.service.impl.CommentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CommentController {

    @Autowired
    CommentServiceImpl commentService;

    @Autowired
    BlogServiceImpl blogService;

    //通过博客id获取评论列表
    @RequestMapping("/queryCommentList")
    @ResponseBody
    public Map<String,Object> getCommentListByBlogID(@RequestParam("blog_id") int blog_id){
        List<Comment> commentList = commentService.getCommentListByBlogID(blog_id);
        HashMap<String, Object> map = new HashMap<>();
        map.put("commentList",commentList);
        map.put("status","ok");
        return map;
    }

    @RequestMapping("/AddComment")
    @ResponseBody
    public Map<String,Object> addComment(@RequestParam("name") String name,
                                         @RequestParam("email") String email,
                                         @RequestParam("content") String content,
                                         @RequestParam("blog_id") int blog_id){

        int id = commentService.getLastCommentId();
        Comment comment = new Comment(id+1, name, email, content, blog_id);
        commentService.addComment(comment);
        Blog blog = blogService.getBlogById(blog_id);
        Integer commentHit = blog.getCommentHit();
        commentService.updateCommentHit(commentHit+1,blog_id);//评论一次就加一
        HashMap<String, Object> map = new HashMap<>();
        map.put("status","ok");
        return map;
    }

    @RequestMapping("/DeleteComment")
    @ResponseBody
    public Map<String,Object> DeleteCommentByID(@RequestParam("id") int id,@RequestParam("blog_id")int blog_id){
        commentService.deleteCommentByID(id);
        Blog blog = blogService.getBlogById(blog_id);
        Integer commentHit = blog.getCommentHit();
        commentService.updateCommentHit(commentHit-1,blog_id);//评论一次就加一
        HashMap<String, Object> map = new HashMap<>();
        map.put("status","ok");
        return map;
    }


    @RequestMapping("/queryAllComment")
    @ResponseBody
    public Map<String,Object> getAllComment(){
        List<Comment> commentList = commentService.getAllComment();
        HashMap<String, Object> map = new HashMap<>();
        HashMap<String, Object> FinaMap = new HashMap<>();
        map.put("commentList",commentList);
        FinaMap.put("data",commentList);
        FinaMap.put("code",0);
        return FinaMap;
    }

    @RequestMapping("/queryCommentByID")
    @ResponseBody
    public Map<String,Object> getCommentByID(@RequestParam("id") int id){
        Comment comment = commentService.getCommentByID(id);
        List<Comment> comments = new ArrayList<>();
        comments.add(comment);
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",comments);
        map.put("code",0);
        return map;
    }



}
